﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class UCProvider : UserControl
    {
        public delegate void UCProviderEventHandler(object sender, UCProviderEventArgs e);
        public event UCProviderEventHandler EventUCProvider;

        private bool EditMode;
        private bool CancellAdd;

        public UCProvider()
        {
            InitializeComponent();
            TxtCode.Tag = "";
            TxtName.Tag = "";
            TxtCode.Text = "";
            TxtName.Text = "";
            EditMode = false;
            CancellAdd = false;
        }
        public int Index
        {
            get;
            set; 
        }
        public string CodeOriginal
        {
            get { return this.TxtCode.Tag.ToString(); }
        }
        public string Code 
        { 
            get { return this.TxtCode.Text;}
            set { this.TxtCode.Tag = value; this.TxtCode.Text = value; }
        }
        public string NameProviderOriginal
        {
            get { return this.TxtName.Tag.ToString(); }
        }
        public string NameProvider
        {
            get { return this.TxtName.Text; }
            set { this.TxtName.Tag = value; this.TxtName.Text = value; }
        }
        public bool AllowAdd
        {
            get { return BntAdd.Enabled; }
            set { this.BntAdd.Tag = value; this.BntAdd.Enabled = value; }
        }
        public bool AllowDelete
        {
            get { return BntDel.Enabled; }
            set { this.BntDel.Tag = value; this.BntDel.Enabled = value; }
        }
        public bool NewProvider { get; set; }
        public void CodeFocus()
        {
            TxtCode.Focus();
        }
        public void NameProviderFocus()
        {
            TxtCode.Focus();
        }

        private void TxtCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
            else
            {
                errorProvider.SetError(LblCode, "");
            }
        }

        private void BntAdd_Click(object sender, EventArgs e)
        {
            TxtCodeValidate();
            TxtNameValidate();
            if (errorProvider.GetError(LblCode) != "")
            {
                this.TxtCode.Focus();
                return;
            }
            if (errorProvider.GetError(LblName) != "")
            {
                this.TxtName.Focus();
                return;
            }
            if (EditMode)
            {
                if (EventUCProvider != null && errorProvider.GetError(LblCode) == "" && errorProvider.GetError(LblName) == "")
                {
                    EditMode = false;
                    EventUCProvider(this, new UCProviderEventArgs(UCProviderActionEnum.Update));
                    BntAdd.Image = Properties.Resources.ImageAdd;
                    TxtCode.Tag = TxtCode.Text;
                    TxtName.Tag = TxtName.Text;
                }
            }
            else
            {
                if (EventUCProvider != null)
                {
                    CancellAdd = false;
                    BntAdd.Enabled = false;
                    EventUCProvider(this, new UCProviderEventArgs(UCProviderActionEnum.Add));
                }
            }
        }
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
        }
        private void BntDel_Click(object sender, EventArgs e)
        {
            if (NewProvider)
            {
                BntDel.Enabled = false;
                BntDel.Image = Properties.Resources.ImageCancel;
                CancellAdd = true;
                EventUCProvider(this, new UCProviderEventArgs(UCProviderActionEnum.CancelAdd));
                return;
            }
            if (EventUCProvider != null)
            {
                BntDel.Enabled = false;
                EventUCProvider(this, new UCProviderEventArgs(UCProviderActionEnum.Remove));
            }
        }

        private void TxtCodeValidate()
        {
            if (TxtCode.Text.Length == 0)
            {
                errorProvider.SetError(LblCode, ResourcesText.CodeRequired );
            }
            else
            {
                errorProvider.SetError(LblCode, "");
            }
        }

        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text.ToLower().Trim() != TxtName.Tag.ToString().ToLower().Trim())
            {
                BntAdd.Image = Properties.Resources.ImageEdit;
                EditMode = true;
                BntAdd.Enabled = true;
            }
            else
            {
                if (TxtCode.Text.ToLower().Trim() != TxtCode.Tag.ToString().ToLower().Trim())
                {
                    BntAdd.Image = Properties.Resources.ImageEdit;
                    EditMode = true;
                    BntAdd.Enabled = true;
                }
                else
                {
                    EditMode = false;
                    BntAdd.Image = Properties.Resources.ImageAdd;
                    BntAdd.Enabled = bool.Parse(BntAdd.Tag.ToString());
                }
            }
            if (TxtName.Text.Trim().Length != 0)
            {
                errorProvider.SetError(LblName, "");
            }
        }

        private void TxtNameValidate()
        {
            if (TxtName.Text.Trim().Length == 0)
            {
                errorProvider.SetError(LblName, ResourcesText.NameRequired);
            }
            else
            {
                errorProvider.SetError(LblName, "");
            }
        }

        private void PnlTemplate_Leave(object sender, EventArgs e)
        {
            if (CancellAdd)
            {
                return;
            }
            TxtCodeValidate();
            TxtNameValidate();
            if (BntAdd.Enabled && !BntDel.Enabled)
            {
                return;
            }
            if (errorProvider.GetError(LblCode) != "")
            { 
                this.TxtCode.Focus();
            }
            if (errorProvider.GetError(LblName) != "")
            {
                this.TxtName.Focus();
            }
        }
        private void TxtCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtCode.Text = TxtCode.Tag.ToString();
            }
        }

        private void TxtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtName.Text = TxtName.Tag.ToString();
            }
        }

        private void TxtCode_TextChanged(object sender, EventArgs e)
        {
            if (TxtCode.Text.ToLower().Trim() != TxtCode.Tag.ToString().ToLower().Trim())
            {
                EditMode = true;
                BntAdd.Image = Properties.Resources.ImageEdit;
                BntAdd.Enabled = true;
            }
            else
            {
                if (TxtName.Text.ToLower().Trim() != TxtName.Tag.ToString().ToLower().Trim())
                {
                    EditMode = true;
                    BntAdd.Image = Properties.Resources.ImageEdit;
                    BntAdd.Enabled = true;
                }
                else
                {
                    BntAdd.Image = Properties.Resources.ImageAdd;
                    EditMode = false;
                    BntAdd.Enabled = bool.Parse(BntAdd.Tag.ToString());
                }
            }
            if (TxtCode.Text.Trim().Length != 0)
            {
                errorProvider.SetError(LblName, "");
            }
        }

        private void UCProvider_Load(object sender, EventArgs e)
        {
            if (NewProvider)
            {
                toolTipOpe.SetToolTip(BntDel, ResourcesText.CancelAdd);
            }
        }
    }
    public enum UCProviderActionEnum
    { 
        Add = 0,
        Update = 1,
        Remove = 2,
        CancelAdd = 3
    }
    public class UCProviderEventArgs : EventArgs
    {
        private UCProviderActionEnum _Action;
        public UCProviderEventArgs(UCProviderActionEnum action)
        {

            _Action = action;
        }
        public UCProviderActionEnum Action { get {return _Action; }}
    }
}
